<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入车型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_common_use_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小区ID" prop="villageId">
        <el-input
          v-model="queryParams.villageId"
          placeholder="请输入小区ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区名称" prop="villageName">
        <el-input
          v-model="queryParams.villageName"
          placeholder="请输入小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入业主姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入业主ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:wx_car:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:wx_car:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:wx_car:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['business:wx_car:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wx_carList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆ID" align="center" prop="carId" />
      <el-table-column label="车牌号" align="center" prop="number" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="车型" align="center" prop="model" />
      <el-table-column label="下发状态" align="center" prop="dispatchStatus">
        <template slot-scope="scope">
          <div :style="{color: statusColorMap[scope.row.remark]}">
            <dict-tag :options="dict.type.dispatch_status_type" :value="scope.row.remark"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="下发时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{parseTime(scope.row.updateTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <div :style="{color: statusColorMap[scope.row.status]}">
            <dict-tag :options="dict.type.review_status_type" :value="scope.row.status"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="小区ID" align="center" prop="villageId" />
      <el-table-column label="小区名称" align="center" prop="villageName" />
      <el-table-column label="业主姓名" align="center" prop="realname" />
      <el-table-column label="业主ID" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.status === '1'"
            size="mini"
            type="text"
            style="color: green"
            icon="el-icon-check"
            @click="handleAction('2', scope.row)"
          >通过</el-button>
          <el-button
            v-show="scope.row.status === '1'"
            size="mini"
            type="text"
            style="color: #f1a63f"
            icon="el-icon-close"
            @click="handleAction('0', scope.row)"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:wx_car:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:wx_car:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆认证管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="车牌号" prop="number">
          <el-input v-model="form.number" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="车型" prop="model">
          <el-input v-model="form.model" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.sys_common_use_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小区ID" prop="villageId">
          <el-input v-model="form.villageId" placeholder="请输入小区ID" />
        </el-form-item>
        <el-form-item label="小区名称" prop="villageName">
          <el-input v-model="form.villageName" placeholder="请输入小区名称" />
        </el-form-item>
        <el-form-item label="业主姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入业主姓名" />
        </el-form-item>
        <el-form-item label="业主ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入业主ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="车牌审核"
      :visible.sync="reviewVis"
      width="30%">
      <el-form :model="reviewForm" ref="reviewForm">
        <el-form-item label="审核理由" prop="reviewContent" :rules="[{
        required: reviewForm.currentStatus === '3' || reviewForm.currentStatus === '4',
        message: '请输入驳回或者废弃理由',
        trigger: 'blur'
      }]">
          <el-input v-model="reviewForm.reviewContent" type="textarea" :rows="6" placeholder="请填写审核理由"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="reviewVis = false">取 消</el-button>
        <el-button :loading="submitReviewFormLoading" type="primary" @click="submitReviewForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listWx_car, getWx_car, delWx_car, addWx_car, updateWx_car, exportWx_car, reviewCar } from "@/api/business/wx_car";

export default {
  name: "Wx_car",
  dicts: ['review_status_type', 'dispatch_status_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆认证管理表格数据
      wx_carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        brand: null,
        model: null,
        status: null,
        villageId: null,
        villageName: null,
        realname: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      reviewForm: {
        currentStatus: '0',
        reviewContent: '',
        bindingId: '',
        wxUserId: ''
      },
      reviewVis: false,
      submitReviewFormLoading: false,
      statusColorMap: {
        '0': 'grey',
        '1': '#f1a63f',
        '2': 'green',
        '3': 'red',
      },
    };
  },
  created() {
    let item = sessionStorage.getItem('user');
    let parse = JSON.parse(item);
    let s = parse.roles.map(item => item.roleKey).join(',');
    if (s.indexOf('property') !== -1) {
      this.queryParams.villageId = parse.villageId;
    }
    this.$nextTick(this.getList)
  },
  methods: {
    /** 查询车辆认证管理列表 */
    getList() {
      this.loading = true;
      listWx_car(this.queryParams).then(response => {
        this.wx_carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        number: null,
        brand: null,
        model: null,
        status: null,
        villageId: null,
        villageName: null,
        realname: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆认证管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carId = row.carId || this.ids
      getWx_car(carId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆认证管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carId != null) {
            updateWx_car(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWx_car(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.carId || this.ids;
      this.$modal.confirm('是否确认删除车辆认证管理编号为"' + carIds + '"的数据项？').then(function() {
        return delWx_car(carIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有车辆认证管理数据项？').then(() => {
        this.exportLoading = true;
        return exportWx_car(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /**
     * 统一处理审批
     */
    handleAction(status, row) {
      this.reviewForm.currentStatus =  status;
      this.reviewVis = true;
      this.reviewForm.userId = row.userId;
      this.reviewForm.recordId = row.carId;
    },
    /**
     * 提交审核数据
     */
    submitReviewForm() {
      this.submitReviewFormLoading = true;
      this.$nextTick(() => {
        this.$refs['reviewForm'].validate(res => {
          if (res) {
            reviewCar({
              status: this.reviewForm.currentStatus,
              content: this.reviewForm.reviewContent,
              recordId: this.reviewForm.recordId,
              userId: this.reviewForm.userId
            }).then(res => {
              if (res.code !== 200) {
                throw new Error();
              }
              this.getList();
              this.submitReviewFormLoading =  false;
              this.reviewVis = false;
              this.reviewForm = {};
              this.$message.success('审核成功！')
            }).catch(e => {
              this.submitReviewFormLoading =  false;
              this.reviewVis = false;
              this.$message.error('审核失败，请重试！')
            })
          } else {
            this.submitReviewFormLoading =  false;
          }
        })
      })
    }
  }
};
</script>
